package com.zry.fertilizer.ui.base

import android.app.Dialog
import android.content.pm.ActivityInfo
import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.os.Build
import android.os.Bundle
import android.support.annotation.RequiresApi
import android.view.Window
import com.zry.fertilizer.animation.RecyclerViewUtilKt
import com.zry.fertilizer.config.MyActivityManager
import com.zry.fertilizer.config.MyApplication
import com.zry.fertilizer.retrofit.ApiSupport
import com.zry.fertilizer.utils.ToastUtil


abstract class BaseActivity : PermissionActivity(), ApiSupport {
    var myApplication: MyApplication? = null
    var recyclerViewUtilKt: RecyclerViewUtilKt? = null
    var myActivityManager: MyActivityManager? = null
    var loadingDialog: Dialog? = null
    var viewDataBinding: ViewDataBinding? = null


    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE)
        viewDataBinding = getDataBinding()

        myActivityManager = MyActivityManager.getActivityManager()
        myActivityManager?.addActivity(this)
        myActivityManager?.addPermissionActivity(this)
        myApplication = MyApplication.instance()
        //禁止横竖屏切换
        requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT

        loadingDialog = ToastUtil.loadingDialog(this)

        initData()
    }


    private fun getDataBinding(): ViewDataBinding {
        return DataBindingUtil.setContentView(this, getLayoutId())
    }

    protected abstract fun getLayoutId(): Int
    protected abstract fun initData()


    protected fun toast(toastMessage: String) {
        ToastUtil.toast(this, toastMessage)
    }


    override fun onDestroy() {
        super.onDestroy()
        myActivityManager?.removePermissionActivity(this)
        myActivityManager?.finishActivity()
    }
}
